package com.atguigu.controller;


import com.atguigu.constant.MqConst;
import com.atguigu.entity.ProductImage;
import com.atguigu.entity.ProductSalePropertyKey;
import com.atguigu.entity.SkuInfo;
import com.atguigu.feign.ProductFeignClient;
import com.atguigu.feign.SearchFeignClient;
import com.atguigu.result.RetVal;
import com.atguigu.service.ProductImageService;
import com.atguigu.service.ProductSalePropertyKeyService;
import com.atguigu.service.SkuInfoService;
import com.atguigu.service.impl.ProductSalePropertyKeyServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 库存单元表 前端控制器
 * </p>
 *
 * @author hachimen
 * @since 2021-08-25
 */
@RestController
@RequestMapping("product")
public class SkuInfoController {

    @Autowired
    private SkuInfoService skuInfoService;
    @Autowired
    private ProductSalePropertyKeyService productSalePropertyKeyService;
    @Autowired
    private ProductImageService productImageService;
    @Autowired
    private SearchFeignClient searchFeignClient;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    //查询SPU属性
    @GetMapping("querySalePropertyByProductId/{productId}")
    public RetVal querySalePropertyByProductId(@PathVariable Long productId){
        List<ProductSalePropertyKey> list = productSalePropertyKeyService.querySalePropertyByProductId(productId);
        return RetVal.ok(list);
    }
    //查询SPU图片
    @GetMapping("queryProductImageByProductId/{productId}")
    public RetVal queryProductImageByProductId(@PathVariable Long productId){
        List<ProductImage> list = productImageService.queryProductImageByProductId(productId);
        return RetVal.ok(list);
    }

    //添加SPU中的SKU
    @PostMapping("saveSkuInfo")
    public RetVal saveSkuInfo(@RequestBody SkuInfo skuInfo){
        skuInfoService.saveSkuInfo(skuInfo);
        return RetVal.ok();
    }

    //分页查询SKU
    @GetMapping("querySkuInfoByPage/{page}/{limit}")
    public RetVal querySkuInfoByPage(@PathVariable Long page,@PathVariable Long limit){
        Page<SkuInfo> skuInfoPage = skuInfoService.querySkuInfoByPage(page,limit);
        return RetVal.ok(skuInfoPage);
    }

    //SKU上架
    @GetMapping("onSale/{id}")
    public RetVal onSale(@PathVariable Long id){
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setId(id);
        skuInfo.setIsSale(1);
        skuInfoService.updateById(skuInfo);
        //searchFeignClient.onSale(id);
        rabbitTemplate.convertAndSend(MqConst.ON_OFF_SALE_EXCHANGE,MqConst.ON_SALE_ROUTING_KEY,id);
        return RetVal.ok();
    }

    //SKU下架
    @GetMapping("offSale/{id}")
    public RetVal offSale(@PathVariable Long id){
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setId(id);
        skuInfo.setIsSale(0);
        skuInfoService.updateById(skuInfo);
        //searchFeignClient.offSale(id);
        rabbitTemplate.convertAndSend(MqConst.ON_OFF_SALE_EXCHANGE,MqConst.OFF_SALE_ROUTING_KEY,id);
        return RetVal.ok();
    }
}

